import 'package:demo_app_video/commons/router/routes.dart';
import 'package:demo_app_video/commons/sp.dart';
import 'package:demo_app_video/commons/toast.dart';
import 'package:demo_app_video/store/user.dart';
import 'package:flutter/material.dart';
import 'package:demo_app_video/widgets/pin_code.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class PasswordDialog extends StatefulWidget {
  const PasswordDialog({Key? key}) : super(key: key);

  @override
  State<PasswordDialog> createState() => _PasswordDialogState();
}

class _PasswordDialogState extends State<PasswordDialog> {
  TextEditingController password1 = TextEditingController(text: "");

  String password = '';

  bool is_one = false;

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: 400.h,
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.only(
              topLeft: Radius.circular(10.sp),
              topRight: Radius.circular(10.sp))),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Container(
            height: 20.h,
          ),
          !is_one
              ? Text(
                  '请输入密码',
                  style: TextStyle(
                    fontSize: 16.sp,
                    fontWeight: FontWeight.w500,
                  ),
                )
              : Text(
                  '确认密码',
                  style: TextStyle(
                    fontSize: 16.sp,
                    fontWeight: FontWeight.w500,
                  ),
                ),
          Container(
            height: 20.h,
          ),
          PinCodeTextField(
              autofocus: true,
              controller: password1,
              defaultBorderColor: Colors.black,
              pinBoxWidth: 40.w,
              pinBoxHeight: 40.w,
              onDone: (text) async {
                var qing = await Sp().getSp(Sp.qing);
                if (qing == '1') {
                  //s何止
                  if (!is_one) {
                    var sp_pass = await Sp().getSp(Sp.qing_pass);
                    if (sp_pass == text) {
                      Sp().setSp(Sp.qing, '0');
                      $UserStore.setQing('0');
                      Sp().setSp(Sp.qing_pass, '');
                      Routes().popNowPage(context);
                    } else {
                      password1.clear();
                      Toast.getInstance().showError(msg: '密码不正确');
                    }
                  }
                } else {
                  //存储到本地
                  //设置
                  if (!is_one) {
                    password = text;
                    is_one = true;
                    password1.clear();
                    setState(() {});
                    return;
                  }

                  if (is_one) {
                    //判断两次密码一致
                    if (password.toString() == text.toString()) {
                      //开启青少年
                      Sp().setSp(Sp.qing, '1');
                      $UserStore.setQing('1');
                      Sp().setSp(Sp.qing_pass, password);
                      Routes().popNowPage(context);
                    } else {
                      is_one = false;
                      password = '';
                      Toast.getInstance().showError(msg: '两次密码不一致');
                      password1.clear();
                      setState(() {});
                    }
                  }
                  // Sp().setSp(Sp.qing, '1');
                }
              })
        ],
      ),
    );
  }
}
